package com.bawei.lib_widget.verify_code

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.bawei.lib_widget.R

/**
 *作者：lxy
 *功能:
 *时间：2025/5/28
 */
class VerifyCodeItemView:FrameLayout {

    private var cursor: View//游标
    private var indicatorView: View//指示器
    private var numberView: TextView//验证码
    private var alphaAnimator: ValueAnimator? = null//透明动画
    private val indicatorColor: Int//指示器颜色
    private val indicatorFocusColor: Int//指示器选中颜色

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: android.util.AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: android.util.AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)

    @SuppressLint("Recycle")
    constructor(
        context: Context,
        attrs: android.util.AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        val parentView =
            LayoutInflater.from(context).inflate(R.layout.layout_verify_code_item, this)

        cursor = parentView.findViewById(R.id.view_cursor)
        indicatorView = parentView.findViewById(R.id.view_focus)
        numberView = parentView.findViewById(R.id.tv_number)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeItemView)

        var cursorColor =
            typedArray.getColor(R.styleable.VerifyCodeItemView_cursorColor, Color.BLACK)
        indicatorColor =
            typedArray.getColor(R.styleable.VerifyCodeItemView_indicatorColor, Color.GRAY)
        indicatorFocusColor =
            typedArray.getColor(R.styleable.VerifyCodeItemView_indicatorFocusedColor, Color.BLUE)

        var numberSize = typedArray.getDimension(
            R.styleable.VerifyCodeItemView_numberSize,
            (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25f, resources.displayMetrics))
        )
        typedArray.recycle()
        cursor.visibility = View.GONE
        cursor.setBackgroundColor(cursorColor)
        numberView.paint.textSize = numberSize
        indicatorView.setBackgroundColor(indicatorColor)
    }

    fun setNumber(number: String) {
        numberView.text = number
    }

    fun setFocus(focus: Boolean) {
        if (focus) {
            onFocused()
        } else {
            onUnfocused()
        }
    }

    private fun onFocused() {
        indicatorView.setBackgroundColor(indicatorFocusColor)
        cursor.visibility = View.VISIBLE
        alphaAnimator = ObjectAnimator.ofFloat(cursor, "alpha", 0f, 1f)
        alphaAnimator!!.duration = 1000
        alphaAnimator!!.repeatMode = ValueAnimator.RESTART
        alphaAnimator!!.repeatCount = -1
        alphaAnimator!!.start()

    }

    private fun onUnfocused() {
        indicatorView.setBackgroundColor(indicatorColor)
        cursor.visibility = View.GONE
        if (alphaAnimator != null) {
            alphaAnimator!!.cancel()
            alphaAnimator = null
        }
    }
}